package com.es.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

//切面就是一个连接点集合
@Component
@Aspect
public class ServiceAspect {
    /***
     * Pointcut 写在方法之上，方法名就是切点的名字
     */

    @Pointcut("execution(* com.es.service..*.*(..))")
    public void servicePC(){

    }

    @Pointcut("@annotation(com.es.annotation.Permission)")
    public void annoPC(){

    }

    @Around("annoPC()")
    public Object beginPermission(ProceedingJoinPoint proceedingJoinPoint){
        Object obj = proceedingJoinPoint.getTarget();
        System.out.println(obj);
       // obj.getClass().get
      //  proceedingJoinPoint.
        System.out.println("我开始验证权限了");
       // System.out.println("权限验证失败：0");
        try {
            return proceedingJoinPoint.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }


    @Before("servicePC()")
    public void beginTranscation() {
        System.out.println("开启事务");
    }

    /*@After("within( com.es.service..*Student*)")*/
    @After("servicePC()")
    public void commitTranscation() {
        System.out.println("提交事务");
    }


    @AfterThrowing(pointcut = "servicePC()", throwing = "ex")
    public void exceptionAdvice(Throwable ex) {
        System.out.println("出异常了");
        System.out.println("事务回滚");
    }

    @AfterReturning(pointcut = "servicePC()", returning = "result")
    public void finallyAdvice(Object result) {
        System.out.println("方法最后的返回值是：" + result);
    }

    @Around("servicePC()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) {
        Object target = joinPoint.getTarget();
        System.out.println("我代理的对象是：" + target);
        try {
           return joinPoint.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }
}
